using System.Web.Mvc;
using System.Web.Routing;

namespace Pronghorn.Core
{
	public abstract class ProngHornControllerBase : Controller
	{
		private IProngHornControllerContext _controllerContext;

		protected ProngHornControllerBase(ViewDataDictionary<IProngHornSkinContext> viewDataDictionary,ISiteContext siteContext, IIocContainer container)
		{
			SiteContext = siteContext;
			Container = container;
			ViewData = viewDataDictionary;
		}

		protected new ViewDataDictionary<IProngHornSkinContext> ViewData
		{
			get; set;
		}

		public IIocContainer Container
		{
			get; private set;
		}

		public new IProngHornControllerContext ControllerContext
		{
			get { return _controllerContext; }
			set 
			{ 
			    _controllerContext = value;
			    base.ControllerContext = _controllerContext as ControllerContext;
			}
		}

		public ISiteContext SiteContext
		{
			get; private set;
		}

		protected override void ExecuteCore()
		{
			TempData.Load(base.ControllerContext, TempDataProvider);
			try
			{
			    string actionName = base.ControllerContext.HttpContext.Request.HttpMethod;
			    if (!ActionInvoker.InvokeAction(base.ControllerContext, actionName))
			    {
			        HandleUnknownAction(actionName);
			    }
			}
			finally
			{
			    TempData.Save(base.ControllerContext, TempDataProvider);
			}
		}

		protected override void Initialize(RequestContext requestContext)
		{
			ControllerContext = new ProngHornControllerContext(requestContext, this, SiteContext);
			TempData = new TempDataDictionary();
		}

		protected override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			ViewData.Model.Areas.Add(new ProngHornArea("LeftNavBar"));
			base.OnActionExecuted(filterContext);
		}

		protected new ProngHornViewResult View()
		{
			return View(null, null, null);
		}

		protected new ProngHornViewResult View(IView view)
		{
			return View(view, null);
		}

		protected new ProngHornViewResult View(object model)
		{
			return View(null, null, model);
		}

		protected new ProngHornViewResult View(string viewName)
		{
			return View(viewName, null, null);
		}

		protected new ProngHornViewResult View(string viewName, object model)
		{
			return View(viewName, null, model);
		}

		protected new ProngHornViewResult View(string viewName, string masterName)
		{
			return View(viewName, masterName,null);
		}

		protected new ProngHornViewResult View(IView view, object model)
		{
			if (model != null)
			{
			    base.ViewData.Model = model;
			}
			var result = new ProngHornViewResult
			                                   {
			                                       View = view,
			                                       ViewData = ViewData,
			                                       TempData = TempData
			                                   };
			return result;
		}

		protected new ProngHornViewResult View(string viewName, string masterName, object model)
		{
			if (model != null)
			{
			    base.ViewData.Model = model;
			}
			var result = new ProngHornViewResult
			                                   {
			                                       ViewName = viewName,
			                                       MasterName = masterName,
			                                       ViewData = ViewData,
			                                       TempData = TempData
			                                   };
			return result;
		}
	}
}